#include<bits/stdc++.h>
using namespace std;
using ll=long long;
const int N=4005;
const int M=2e5+5;
int T;
int n,m;
int x[N],y[N];
ll o[N],ans;
bool p[M];
int main(){
    freopen("chess.in","r",stdin);
    freopen("chess.out","w",stdout);
    p[1]=1;
    for(int i=2;i<M;++i){
        if(!p[i]){
            for(int j=2;i*j<M;++j)p[i*j]=1;
        }
    }
    cin>>T;
    while(T--){
        cin>>n>>m;
        for(int i=1;i<=n;++i)cin>>x[i]>>y[i];
        for(int i=1;i<=n;++i)cin>>o[i];
        ans=0;
        for(int i=1;i<=n;++i){
            for(int j=i+1;j<=n;++j){
                for(int k=j+1;k<=n;++k){
                    vector<int> v;
                    v.push_back(abs(x[i]-x[j])+abs(y[i]-y[j]));
                    v.push_back(abs(x[i]-x[k])+abs(y[i]-y[k]));
                    v.push_back(abs(x[k]-x[j])+abs(y[k]-y[j]));
                    sort(v.begin(),v.end());
                    if(p[v[1]])continue;
                    if(v[1]<=10||v[1]%20==2){
                        ans+=o[i]+o[j]+o[k];
                    }
                }
            }
        }
        cout<<ans<<'\n';
    }
    return 0;
}
